package com.neborosoft.lib.collections;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;

/**
 * Project: FlyingDog
 * User: stikhonenko
 * Date: 2/25/13
 * Time: 6:14 PM
 */
public class ObjectIndexingMap<T> {
    private static final float LOAD_FACTOR = 1.1f;

    private T[] objects;
    private HashMap<T,Integer> objectIndexMap;

    public ObjectIndexingMap(T[] objects){
        this.objects = objects;
        objectIndexMap = new HashMap<T, Integer>(objects.length * 2, LOAD_FACTOR);

        for(int i = 0; i < objects.length; i++){
            objectIndexMap.put(objects[i],i);
        }
    }

    public T getObjectByIndex(int index){
        return objects[index];
    }

    public Integer getIndexOfObject(T object){
        return objectIndexMap.get(object);
    }

    public List<T> toList(){
        return Collections.unmodifiableList(Arrays.asList(objects));
    }
}
